package com.x.ant.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.ant.dao.UserDao;
import com.x.ant.manage.UserManage;
import com.x.ant.model.DeptUser;
import com.x.ant.model.Sender;
import com.x.ant.model.Staff;
import com.x.ant.model.User;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import com.x.security.model.Account;
import com.x.security.rpc.SecurityRpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserService extends BaseService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private UserManage userManage;

    @Reference(interfaceName="com.x.security.rpc.SecurityRpcService")
    private SecurityRpcService securityRpcService;

    public List<User> selectUserList(DeptUser deptUser) {
        try {
            return userDao.selectUserList(deptUser);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public int selectUserNum(DeptUser deptUser) {
        try {
            return userDao.selectUserNum(deptUser);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Staff> selectStaffList(Staff staff) {
        try {
            return userDao.selectStaffList(staff);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Sender> selectSenderList(Sender sender) {
        try {
            return userDao.selectSenderList(sender);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public int selectSenderNum(Sender sender) {
        try {
            return userDao.selectSenderNum(sender);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public Staff insertStaff(Staff staff, String deptParentId) {
        try {
            return userManage.insertStaff(staff, deptParentId);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public Sender insertSender(Sender sender) {
        try {
            return userManage.insertSender(sender);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public int selectAccountNum(Account account) {
        try {
            return securityRpcService.selectAccountNum(account);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void updateAccountPassword(Account account) {
        try {
            securityRpcService.updateAccount(account);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

}
